package com.yyw.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.Class;
import com.yyw.bean.User;
import com.yyw.dao.ClassDao;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

@Service
public class ClassService {

    @Autowired
    private ClassDao classDao;

    public Result saveClass(Class clas) {
        if (StringUtils.isEmpty(clas.getName()) || clas.getUserid() == null) {
            return ResultFactory.notice("数据不合法");
        }

        int i = classDao.saveClass(clas);
        if (i > 0) {
            return ResultFactory.success("添加成功");
        } else {
            return ResultFactory.failure("添加失败");
        }
    }

    public Result getUsersByClassId(Long classid) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }

        List<User> classList = classDao.getUsersByClassId(classid, null);

        return ResultFactory.success(classList);
    }

    public Result getClassUsers(Long classid, Integer pageNo, Integer pageNum, String search) {
        // 数据校验
        if (classid == null) {
            return ResultFactory.notice("难度不合法");
        }
        if (pageNo == null || (pageNo < 1)) {
            return ResultFactory.notice("页数不合法");
        }
        if (pageNum == null || pageNum < 1) {
            return ResultFactory.notice("每页数量不合法");
        }
        if (!StringUtils.isEmpty(search)) {
            search = "%" + search + "%";
        }

        PageHelper.startPage(pageNo, pageNum);
        List<User> users = classDao.getUsersByClassId(classid, search);

        PageInfo<User> pageInfo = new PageInfo<>(users);
        return ResultFactory.success(pageInfo);
    }
}
